package com.lins.framework.utils.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lins.framework.R;


/**
 * Created by Admin on 2017/4/21.
 */

public class MyAlertDialogFragment extends DialogFragment implements View.OnClickListener {

    public static MyAlertDialogFragment newInstance(String title, String message, String negative, String positive) {

        Bundle args = new Bundle();

        MyAlertDialogFragment fragment = new MyAlertDialogFragment();
        args.putString("alert-title", title);
        args.putString("alert-message", message);
        args.putString("alert-negative", negative);
        args.putString("alert-positive", positive);
        fragment.setArguments(args);
        return fragment;
    }

    private String getTitle() {
        return getArguments().getString("alert-title");
    }

    private String getMessage() {
        return getArguments().getString("alert-message");
    }

    private String getNegative() {
        return getArguments().getString("alert-negative");
    }

    private String getPositive() {
        return getArguments().getString("alert-positive");
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_my_alert, null);

        TextView idTvTitle = (TextView) view.findViewById(R.id.id_tv_title);
        TextView idTvMsg = (TextView) view.findViewById(R.id.id_tv_msg);
        Button idBtnNegative = (Button) view.findViewById(R.id.id_btn_negative);
        Button idBtnPositive = (Button) view.findViewById(R.id.id_btn_positive);

        idTvTitle.setText(getTitle());
        idTvMsg.setText(getMessage());
        idBtnNegative.setText(getNegative());
        idBtnPositive.setText(getPositive());

        idBtnNegative.setOnClickListener(this);
        idBtnPositive.setOnClickListener(this);

        builder.setView(view);
        return builder.create();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_btn_negative:
                mOnBtnClickListener.onCancelClickListener();
                dismiss();
                break;
            case R.id.id_btn_positive:
                mOnBtnClickListener.onOkClickListener();
                dismiss();
                break;
        }
    }

    private OnBtnClickListener mOnBtnClickListener;

    public interface OnBtnClickListener {
        void onCancelClickListener();

        void onOkClickListener();
    }


    public void setOnBtnClickListener(OnBtnClickListener mOnBtnClickListener) {
        this.mOnBtnClickListener = mOnBtnClickListener;
    }

}